//
//  TravelDirectSeedingVC.m
//  mafengwo
//
//  Created by Mac on 16/4/14.
//  Copyright © 2016年 Mac. All rights reserved.
//

#import "TravelDirectSeedingVC.h"
#import "SegmentView.h"
#import "TravelTableViewCell.h"
#import "CommentsModel.h"
@interface TravelDirectSeedingVC ()<UINavigationControllerDelegate, UIImagePickerControllerDelegate,UITableViewDataSource,UITableViewDelegate>{

    SegmentView *_segmentView;
    UITableView *_latestTableView;
    UITableView *_chooseTableView;
    /*
     1.因为无法蚂蜂窝的数据加密的很好，很难反加密破解，所以数据源暂时只能自己写了，应该是拿到一个json或者是XML，解析就行，我自己封装得有这两种数据的解析类和基类Model
     */
    //图片数据源
    NSMutableArray *_dataList;
    //图片数据源反向数组
    NSMutableArray *_reversedataList;

    //评论数据源
    NSMutableArray *_commentsDataList;

    //评论数据源反向数组
    NSMutableArray *_reversecommentsDataList;


}

@end

@implementation TravelDirectSeedingVC



- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"青岛";
    self.view.backgroundColor = [UIColor whiteColor];
    //导航栏上的返回摁钮 导航栏背景等属性设置
    UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 30, 30)];
    [button setImage:[UIImage imageNamed:@"back_navig.png"] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(backActon) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *item = [[UIBarButtonItem alloc]initWithCustomView:button];
    self.navigationItem.leftBarButtonItem = item;
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"backg_navi.png"] forBarMetrics:UIBarMetricsDefault];

    //加载数据
    [self loadData];
    [self createSubViews];

}
-(void)loadData{
    NSString *pathStr = [[NSBundle mainBundle]pathForResource:@"qingdaodemo" ofType:@"plist"];
    NSArray *arr = [NSArray arrayWithContentsOfFile:pathStr];
    _dataList = [[NSMutableArray alloc]initWithCapacity:1];
    for (NSDictionary *dic in arr) {
        //放的是图片的URL
        [_dataList addObject:[dic objectForKey:@"img"]];
    }

    //评论数据源
    //1.通过数据解析类获得根数句结构
    _commentsDataList = [[NSMutableArray alloc]initWithCapacity:1];
    NSDictionary *dic = [DataParser jsonObjectFromFileName:@"new_comment"];
    NSArray *arr2 = [dic objectForKey:@"new"];
    for (NSDictionary *dic in arr2) {
        CommentsModel *commentsModel = [[CommentsModel alloc]initWithDic:dic];
        [_commentsDataList addObject:commentsModel];
    }

    //图片数据源反向数组
    _reversedataList = [[NSMutableArray alloc]initWithCapacity:1];
    for (int i = (int)_dataList.count-1; i>=0; i--) {

        [_reversedataList addObject:_dataList[i]];
    }

    _reversecommentsDataList = [[NSMutableArray alloc]initWithCapacity:1];
    for (int i = (int)_commentsDataList.count-1; i>=0; i--) {

        [_reversecommentsDataList addObject:_commentsDataList[i]];
    }

}
-(void)createSubViews{


    //分部视图，最新和精选
    NSArray *array = @[@"最新",@"精选"];
    _segmentView = [[SegmentView alloc]initWithFrame:CGRectMake(0, 0, 414, 40-5) withTitles:array segmentBlock:^(int index) {

        if (index == 0) {

            [UIView animateWithDuration:.35 animations:^{

                _latestTableView.left = 0;
                _chooseTableView.left = _latestTableView.right;
            }];


        }
        if (index == 1) {
            [UIView animateWithDuration:.35 animations:^{

                _latestTableView.left = -KScreenW;
                _chooseTableView.left = _latestTableView.right;
            }];

        }
    }];

    _segmentView.backgroundColor = [UIColor whiteColor];
    _segmentView.selectedIndex = 0;// 默认显示评论列表

    //相机
    UIButton *camera = [[UIButton alloc]initWithFrame:CGRectMake(KScreenW/2-30,KScreenH - 160, 60, 60)];
    [camera setImage:[UIImage imageNamed:@"camera"] forState:UIControlStateNormal];
    camera.layer.cornerRadius = 30;
    camera.layer.borderColor = [UIColor whiteColor].CGColor;
    camera.layer.borderWidth = 3;
    camera.clipsToBounds = YES;
    [camera addTarget:self action:@selector(openPhoto) forControlEvents:UIControlEventTouchUpInside];


    //两个横向连接的tableview，火车跑得快，全靠车头带
    //最新
    _latestTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, KScreenW, KScreenH-30) style:UITableViewStyleGrouped];
//    _latestTableView.top = _segmentView.bottom;
    _latestTableView.delegate = self;
    _latestTableView.dataSource = self;
    _latestTableView.rowHeight = 400;
    [self.view addSubview:_latestTableView];

    //精选
    _chooseTableView = [[UITableView alloc]initWithFrame:CGRectMake(_latestTableView.right, 30, KScreenW, KScreenH) style:UITableViewStylePlain];
    _chooseTableView.delegate = self;
    _chooseTableView.dataSource = self;
    _chooseTableView.rowHeight = 400;
    [self.view addSubview:_chooseTableView];
    [self.view addSubview:_segmentView];
    [self.view addSubview:camera];

}

//打开相册
-(void)openPhoto{
    //创建pickerController对象
    UIImagePickerController *imgVC = [[UIImagePickerController alloc] init];

    //设置图片来源
//    imgVC.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
    imgVC.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;


    //设置代理
    imgVC.delegate = self;

    //弹出imgVC
    [self presentViewController:imgVC animated:YES completion:nil];
}

-(void)backActon{

    [self dismissViewControllerAnimated:NO completion:^{

    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark- tableView delegate
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

//    return 10;

    if (tableView == _chooseTableView) {
        return 10;
    }
    return _dataList.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (tableView == _latestTableView) {


    TravelTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"DirectSeeding_cell"];

    if (cell == nil) {
        NSLog(@"要创建新的单元格了");
        cell = [[[NSBundle mainBundle]loadNibNamed:@"TravelTableViewCell" owner:nil options:nil]lastObject];
    }
    NSString *url = [_dataList objectAtIndex:indexPath.row];

    [cell.imgV setImageWithURL:[NSURL URLWithString:url]];

    if (indexPath.row<=_commentsDataList.count) {

    cell.commentsModel  = _commentsDataList[indexPath.row];

    }
    if (indexPath.row>_commentsDataList.count) {
        cell.commentsModel = _commentsDataList[6];
    }

        return cell;

    }
    else  {
        TravelTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"DirectSeeding_cell"];

        if (cell == nil) {
            NSLog(@"要创建新的单元格了");
            cell = [[[NSBundle mainBundle]loadNibNamed:@"TravelTableViewCell" owner:nil options:nil]lastObject];
        }
        NSString *url = [_reversedataList objectAtIndex:indexPath.row];

        [cell.imgV setImageWithURL:[NSURL URLWithString:url]];

        if (indexPath.row<=_reversecommentsDataList.count) {

            cell.commentsModel  = _reversecommentsDataList[indexPath.row];

        }
        if (indexPath.row>_reversecommentsDataList.count) {
            cell.commentsModel = _reversecommentsDataList[6];
        }

        return cell;

    }


//    return cell;
}


@end
